package com.ycxy.sh.transaction.controller;

import com.alibaba.fastjson.JSONObject;
import com.ycxy.sh.transaction.dao.entity.chat.*;
import com.ycxy.sh.transaction.ex.ServiceException;
import com.ycxy.sh.transaction.service.ChatmsgService;
import com.ycxy.sh.transaction.service.FriendsService;
import com.ycxy.sh.transaction.service.UserInfoService;
import com.ycxy.sh.transaction.web.JsonResult;
import com.ycxy.sh.transaction.dao.entity.UserInfo;
import com.ycxy.sh.transaction.service.impl.ChatmsgServiceImpl;
import com.ycxy.sh.transaction.service.impl.FriendsServiceImpl;
import com.ycxy.sh.transaction.web.ServiceCode;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 聊天类
 * 上传下载图片
 */
@Slf4j
@Api(tags = "6.图片管理模块")
@Controller
public class ChatCtrl {
    @Autowired
    FriendsService friendsService;
    @Autowired
    UserInfoService mineService;
    @Autowired
    ChatmsgService chatmsgService;

    /**
     * 上传聊天图片
     * **/
    @PostMapping(value = "/chat/upimg")
    @ResponseBody
    public JSONObject upimg(@RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        JSONObject res = new JSONObject();
        JSONObject resUrl = new JSONObject();
        String filename = UUID.randomUUID().toString().replaceAll("-", "");
        String ext = FilenameUtils.getExtension(file.getOriginalFilename());//获得文件扩展名
        String filenames = filename + "." + ext;
        file.transferTo(new File("D:\\secondshop\\file\\" + filenames));
        resUrl.put("src", "/pic/" + filenames);
        res.put("msg", "");
        res.put("code", 0);
        res.put("data", resUrl);
        return res;
    }
    /**
     * 上传聊天文件
     * **/
    @PostMapping(value = "/chat/upfile")
    @ResponseBody
    public JSONObject upfile(@RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        JSONObject res = new JSONObject();
        JSONObject resUrl = new JSONObject();
        String filename = UUID.randomUUID().toString().replaceAll("-", "");
        String ext = FilenameUtils.getExtension(file.getOriginalFilename());//获得文件扩展名
        String filenames = filename + "." + ext;
        file.transferTo(new File("D:\\secondshop\\file\\" + filenames));
        resUrl.put("src", "/pic/" + filenames);
        resUrl.put("name",file.getOriginalFilename());
        res.put("msg", "");
        res.put("code", 0);
        res.put("data", resUrl);
        return res;
    }

    /**
     * 添加好友跳转到个人中心聊天
     * */
    @PostMapping("/addfrend/{fuserid}")
    @ResponseBody
    public JsonResult addfrend(@PathVariable("fuserid") Long fuserid, HttpSession session){
        Long userid = (Long)session.getAttribute("userid");
        if(userid.equals(fuserid)){
            //不能对自己的商品感兴趣
            String message="操作失败,不能对自己的商品感兴趣哦";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Friends friends=new Friends().setUserId(userid).setFuserId(fuserid);
        Integer integer = friendsService.JustTwoUserIsFriend(friends);
        if(integer==null){
            //如果不存在好友关系插入好友关系
            friendsService.insertFriend(friends);
            friendsService.insertFriend(new Friends().setFuserId(userid).setUserId(fuserid));
        }
       return JsonResult.ok("正在跳转到聊天界面");
    }

    /**
     * TODO 跳转到聊天记录界面
     * */
    @GetMapping("/tochatlog")
    public String tochatlog(){
        return "/user/chat/chatlog";
    }
    /**
     * TODO 查询聊天记录
     * */
    @GetMapping("/chatlog/{uid}")
    @ResponseBody
    public List<UserInfo> chatlog(@PathVariable("uid")Long uid,HttpSession session){
        Long userid=(Long) session.getAttribute("userid");
        List<UserInfo> mines = chatmsgService.LookChatMsg(new ChatMsg().setSendUserId(userid).setReciveUserId(uid));
        return mines;
    }
    /**
     * TODO 初始化聊天
     * */
    @GetMapping("/initim")
    @ResponseBody
    public InitImVo initim(HttpSession session){
        Long userid = (Long) session.getAttribute("userid");
        InitImVo initImVo=new InitImVo();
        //个人信息
        UserInfo mine= friendsService.LookUserMine(userid);
        //好友列表
        List<UserInfo> list= friendsService.LookUserFriend(userid);
        Friend friend=new Friend().setId(2L).setGroupname("分组").setList(list);
        List<Friend> friendList=new ArrayList<>();
        friendList.add(friend);
        //群组信息
        List<Groups> groupList=new ArrayList<>();
        //Data数据
        ImData imData=new ImData().setMine(mine).setFriend(friendList).setGroup(groupList);
        initImVo.setCode(0).setMsg("").setData(imData);
        return initImVo;
    }
}
